	
function Get-BTSConnectionString ($server)
{
	$group = Get-WmiObject MSBTS_GroupSetting -n root\MicrosoftBizTalkServer -computername $server
	$dbName = $group.MgmtDBName
	$dbserver = $group.MgmtDBServerName
	[System.String]::Concat("server=", $dbserver, ";database=", $dbName, ";Integrated Security=SSPI")
}

# Remote execution where this is required will be a problem
function Load-BiztalkExloperOM ($server)
{
	[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.BizTalk.ExplorerOM")
	$catalog = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer
	$catalog.ConnectionString = Get-BTSConnectionString $server
	return $catalog
}

function export-bindings ([string]$bindingFileDest, [string]$BizTalkApp, $server)
{
 		$title = "Export Bindings"	
    if(Test-Path $bindingFileDest)
	 	{
			$message = "A binding file already exist at $bindingFileDest. Do you wish to create a new one?"		
		}
		else
		{
		 $message = "Export Binding of $BizTalkApp to $bindingFileDest. Proceed?"
		}
		if (-not $NoPrompt)
		{
			$Yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
			"Export Bindings for $BizTalkApp"
			$No = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
			"Skip Exporting of Bindings"
			$Exit = New-Object System.Management.Automation.Host.ChoiceDescription "&Exit", `
			"Exit Deployment"
			$options = [System.Management.Automation.Host.ChoiceDescription[]]($Yes, $No, $Exit)
			$result = $host.ui.PromptForChoice($title, $message, $options, 1) 
			switch ($result)
			{
				0 {$exportBinding = $TRUE}
				1 {$exportBinding = $FALSE}
				2 {Exit}
			}
		}

	if ($exportBinding -or $noPrompt)
	{
		if(Is-LocalMachine $server)
		{
			$btsoutput = BTSTask.exe ExportBindings /Destination:$bindingFileDest /ApplicationName:$BizTalkApp /GlobalParties
			
		}
		else
		{
			$btsoutput = psexec "\\$server" BTSTask.exe ExportBindings /Destination:$bindingFileDest /ApplicationName:$BizTalkApp /GlobalParties 2>$null
		}
		
		$btsoutput | ForEach-Object {log-message $_}
		if ($btsoutput -match "Command failed")
		{
		 Throw "Export Binding Failed"
		}
		else
		{
			Log-message "Binding file exported to $bindingFileDest"
			$bak = "{1}-{0:MM.dd.yy.HH.mm.ss}.bakup" -f [datetime]::Now, "$bindingFileName"
			Log-message "Backup Binding file $bak created" -fore green
			Copy-Item $bindingFileDest $(Join-Path $deploymentFolder $bak)
		}
	}

	return $bindingFileDest
}

function import-bindings ($BindingFilePath, $BizTalkApp, $server )
{
	if(Is-LocalMachine $server)
	{
		$btsoutput = BTSTask.exe ImportBindings /ApplicationName:$BizTalkApp /Source:$BindingFilePath
	}
	else
	{
		if (Test-Path $BindingFilePath)
		{
			$btsoutput = psexec "\\$server" BTSTask.exe ImportBindings /ApplicationName:$BizTalkApp /Source:$BindingFilePath 2>$null
		}
		else
		{
			Throw "Unable to locate Binding file at $BindingFilePath"
		}
	}
	$btsoutput | ForEach-Object {log-message $_}
	if ($btsoutput -match "Command failed")
	{
		Throw "Import Binding Failed"
	}
}
function Get-BizTalkStatus ($server)
{
	$cmdstr = "echo . | Powershell -noprofile -noninteractive -command "
	$argstr = "C:\RemoteBizTalkActions.ps1 -Biztalkapp $BizTalkApp -server $($primaryserver) -BizTalkStatus"
	$BiztalkSatusStr = $cmdstr + $argstr
	
	if(Is-LocalMachine $server)
	{
		$tempoutput = cmd /c $BiztalkSatusStr
	}
	else
	{		
		$tempoutput = psexec "\\$($primaryserver)" cmd /c $BiztalkSatusStr 2>$null
	}
	
	if ($tempoutput -match "true")
	{
		return $true
	}
	else
	{
		return $false
	}
		
}